//
//  XMBaseViewController.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/7.
//

#import "XMBaseViewController.h"
#import <IQKeyboardManager/IQKeyboardManager.h>

@interface XMBaseViewController () <UIGestureRecognizerDelegate>

@end

@implementation XMBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self baseInit];
    // Do any additional setup after loading the view.
}

- (void)baseInit {
    [IQKeyboardManager sharedManager].enable = YES;
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    self.view.backgroundColor = kColorHEX(0xffffff);
    if (@available(iOS 13.0, *)) {
            UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
            [appearance configureWithOpaqueBackground];
            appearance.titleTextAttributes=[NSDictionary dictionaryWithObject:[UIColor blackColor] forKey:NSForegroundColorAttributeName];
            appearance.backgroundColor = [UIColor whiteColor];
    //        appearance.shadowColor = [UIColor clearColor] ;
            self.navigationItem.standardAppearance = appearance;
            self.navigationItem.scrollEdgeAppearance = self.navigationItem.standardAppearance;
        }
    if (@available(iOS 11, *)) {
        if (!self.hideNavigationBar && self.navigationController) {
            // 不是rootViewController
            NSArray *arrray = self.navigationController.viewControllers;
            if (![self isEqual:[arrray firstObject]]) {
                
//                [self addNavigationItemWithImageName:@"返回键" itemType:kNavigationItemTypeLeft action:@selector(back)];
            }
        }
    }
    
    if (self.navigationController.viewControllers.count > 1) {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_icon_black"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    }
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }
//    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer == self.navigationController.interactivePopGestureRecognizer) {
        if (self.navigationController.viewControllers.count > 1) {
            [self.navigationController popViewControllerAnimated:YES];
            return NO;
        }
    }
    return YES;
}

- (void)backAction:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)hideNavigationBar:(BOOL)isHide
                 animated:(BOOL)animated {
    if (animated) {
        [UIView animateWithDuration:0.25 animations:^{
            self.navigationController.navigationBarHidden = isHide;
        }];
    }
    else {
        self.navigationController.navigationBarHidden = isHide;
    }
}

//- (void)configKeyboard{
//
//    IQKeyboardManager *keyboardManager = [IQKeyboardManager sharedManager]; // 获取类库的单例变量
//
//    keyboardManager.enable = YES; // 控制整个功能是否启用
//
//    keyboardManager.shouldResignOnTouchOutside = YES; // 控制点击背景是否收起键盘
//
//    keyboardManager.shouldToolbarUsesTextFieldTintColor = YES; // 控制键盘上的工具条文字颜色是否用户自定义
//
//    keyboardManager.toolbarManageBehaviour = IQAutoToolbarByPosition; // 有多个输入框时，可以通过点击Toolbar 上的“前一个”“后一个”按钮来实现移动到不同的输入框
//
//    keyboardManager.enableAutoToolbar = YES; // 控制是否显示键盘上的工具条
//
//    keyboardManager.shouldShowToolbarPlaceholder = YES; // 是否显示占位文字
//
//    keyboardManager.placeholderFont = [UIFont boldSystemFontOfSize:14]; // 设置占位文字的字体
//
//    keyboardManager.keyboardDistanceFromTextField = 10.0f; // 输入框距离键盘的距离
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
